package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.happyscan138.makefun.BaseAdapter.my_release_video;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.base.VideoPlayView;
import com.happyscan138.makefun.base.http;
import com.happyscan138.makefun.more_myself;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.share.access_key;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/1.
 */

public class myReleaseVIDEOAdapter extends BaseAdapter implements VideoPlayView.MediaPlayerImpl {
    private VideoPlayView playView;

    public myReleaseVIDEOAdapter(Context mContext, List<my_release_video> data) {
        this.data = data;
        this.mContext = mContext;
    }
    private int userID;
    private Context mContext;
    private List<my_release_video> data;
    private boolean isSelected;
    private int currentPosition = -1;
    private boolean isPlaying;
    private View currentItemView;
    private my_release_video bean;
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder_myRelease_VIDEO viewHolder;
        //判断converView是否为空
        if(view==null){
            viewHolder=new ViewHolder_myRelease_VIDEO();
            view=View.inflate(mContext, R.layout.myselease_video_item,null);
            viewHolder.myrelease_VIDEO_head=(RoundImageView) view.findViewById(R.id.Myrelease_Saobo_head);
            viewHolder.myrelease_VIDEO_name=(TextView) view.findViewById(R.id.Myrelease_Saobo_name);
            viewHolder.myrelease_VIDEO_time=(TextView) view.findViewById(R.id.Myrelease_Saobo_time);
            viewHolder.myrelease_VIDEO_text=(TextView) view.findViewById(R.id.Myrelease_Saoboo_text);
            viewHolder.myrelease_VIDEO_notice=(ImageButton) view.findViewById(R.id.Myrelease_Saoboe_notice);
            viewHolder.myrelease_VIDEO_btn=(ImageView)view.findViewById(R.id.Myrelease_Saobo_play_btn);
            viewHolder.myrelease_VIDEO_layout=(FrameLayout)view.findViewById(R.id.Myrelease_Saobo_layout);
            viewHolder.myrelease_VIDEO_view=(VideoPlayView)view.findViewById(R.id.Myrelease_Saoboo_play_view);
            viewHolder.myrelease_VIDEO_viewNum=(TextView) view.findViewById(R.id.Myrelease_Saobo_viewNum);
            viewHolder.myrelease_VIDEO_answerNum=(TextView) view.findViewById(R.id.Myrelease_Saobo_answerNum);
            viewHolder.myrelease_VIDEO_commentNum=(TextView) view.findViewById(R.id.Myrelease_Saobo_commentNum);
            viewHolder.myrelease_VIDEO_goodNum=(TextView) view.findViewById(R.id.Myrelease_Saobo_goodNum);

            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder = (ViewHolder_myRelease_VIDEO) view.getTag();
        }
        bean=data.get(position);
            //通过viewHolder找到对应控件，避免每次通过findViewById找控件

        if(bean!=null) {
            userID = bean.userID;
            //通过viewHolder找到对应控件，避免每次通过findViewById找控件
            Picasso.with(mContext).load(bean.icon).into(viewHolder.myrelease_VIDEO_head);
            viewHolder.myrelease_VIDEO_head.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, more_myself.class);
                    mContext.startActivity(intent);
                }
            });
            viewHolder.myrelease_VIDEO_name.setText(bean.name);
            log.e("bean.time", bean.time);
            viewHolder.myrelease_VIDEO_time.setText(bean.time);
            final ViewHolder_myRelease_VIDEO finalViewHolder_Movie = viewHolder;
            viewHolder.myrelease_VIDEO_notice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("buttonnotice", "sdsdsd");
                    isSelected = !isSelected;
                    finalViewHolder_Movie.myrelease_VIDEO_notice.setBackgroundResource(isSelected ? R.mipmap.benotice : R.mipmap.notic);
                    http.client = new AsyncHttpClient();
                    http.params = new RequestParams();
                    http.params.put("access_key", access_key);
                    log.e("access_key", access_key);
                    http.params.put("focus_u_id", userID);
                    String a = userID + "";
                    log.e("focus_u_id", a);
                    log.e("params", http.params.toString());
                    RequestHandle handle = http.client.post(Urls.Notice, http.params, new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            log.e("onSuccess", response.toString());
                            String jsonstr = response.toString();
                            try {
                                JSONObject jsonObject = new JSONObject(jsonstr);
                                String msg = jsonObject.getString("msg");
                                Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                                String code = jsonObject.getString("code");
                                int code_int = Integer.valueOf(code).intValue();
                                if (code_int == 163) {
                                    finalViewHolder_Movie.myrelease_VIDEO_notice.setBackgroundResource(R.mipmap.benotice);
                                } else {
                                    finalViewHolder_Movie.myrelease_VIDEO_notice.setBackgroundResource(R.mipmap.notic);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            super.onFailure(statusCode, headers, responseString, throwable);
                            log.e("onFailure", responseString);
                        }
                    });
                }
            });
            viewHolder.myrelease_VIDEO_text.setText(bean.text);
            viewHolder.myrelease_VIDEO_btn.setOnClickListener(new MyClick(position,
                    viewHolder.myrelease_VIDEO_view, viewHolder.myrelease_VIDEO_layout, view));
            if (currentPosition == position) {
                viewHolder.myrelease_VIDEO_view.setVisibility(View.VISIBLE);

            } else {
                viewHolder.myrelease_VIDEO_view.setVisibility(View.GONE);
                viewHolder.myrelease_VIDEO_layout.setVisibility(View.VISIBLE);
                viewHolder.myrelease_VIDEO_view.stop();
            }
            viewHolder.myrelease_VIDEO_viewNum.setText(bean.viewNum + "");
            viewHolder.myrelease_VIDEO_answerNum.setText(bean.answerNum + "");
            viewHolder.myrelease_VIDEO_commentNum.setText(bean.commentNum + "");
            viewHolder.myrelease_VIDEO_goodNum.setText(bean.goodNum + "");
        }

        return view;
    }

    @Override
    public void onError() {

    }

    @Override
    public void onExpend() {

    }

    @Override
    public void onShrik() {

    }

    class MyClick implements View.OnClickListener {
        private int position;
        private VideoPlayView playView;
        private FrameLayout show_layout;
        private View convertView;

        public MyClick(int position, VideoPlayView playView, FrameLayout show_layout, View convertView) {
            this.position = position;
            this.show_layout = show_layout;
            this.playView = playView;
            this.convertView = convertView;
        }

        @Override
        public void onClick(View v) {
            isPlaying = true;
            currentPosition = position;
            show_layout.setVisibility(View.GONE);
            playView.setUrl(bean.image);
            currentItemView = convertView;
            setPlayView(playView);
            playView.openVideo();
            notifyDataSetChanged();
        }
    }
    public void setPlayView(VideoPlayView playView) {
        this.playView = playView;
        playView.setMediaPlayerListenr(this);
        Log.i("XX", currentPosition + "");
        int[] curr = new int[2];
        currentItemView.getLocationOnScreen(curr);
        Log.i("TAG", curr[1] + "");
    }
}
class ViewHolder_myRelease_VIDEO{
    public RoundImageView myrelease_VIDEO_head;
    public TextView myrelease_VIDEO_name;
    public TextView myrelease_VIDEO_time;
    public TextView myrelease_VIDEO_text;
    public ImageButton myrelease_VIDEO_notice;
    public ImageView myrelease_VIDEO_btn;//播放按钮
    public FrameLayout myrelease_VIDEO_layout;
    public VideoPlayView myrelease_VIDEO_view;
    public TextView myrelease_VIDEO_viewNum;
    public TextView myrelease_VIDEO_answerNum;
    public TextView myrelease_VIDEO_commentNum;
    public TextView myrelease_VIDEO_goodNum;

}